package ex.mlist;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MlistActivity extends Activity {
	ListView musiclist;
	Cursor musiccursor;
	int music_column_index;
	int count;
	MediaPlayer mMediaPlayer;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init_phone_music();
	}

	private void init_phone_music() {
		System.gc();
		String[] proj = { MediaStore.Audio.Media._ID,
				MediaStore.Audio.Media.DATA,
				MediaStore.Audio.Media.DISPLAY_NAME,
				MediaStore.Video.Media.SIZE };
		musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
				proj, null, null, null);
		count = musiccursor.getCount();
		musiclist = (ListView) findViewById(R.id.PhoneMusicList);
		musiclist.setAdapter(new MusicAdapter(getApplicationContext()));

		musiclist.setOnItemClickListener(musiclistener);
		mMediaPlayer = new MediaPlayer();
	}

	private OnItemClickListener musiclistener = new OnItemClickListener() {
		public void onItemClick(AdapterView parent, View v, int position,
				long id) {
			System.gc();

			music_column_index = musiccursor
					.getColumnIndex(MediaStore.Audio.Media.DATA);
			musiccursor.moveToPosition(position);
			String filename = musiccursor.getString(music_column_index);

			music_column_index = musiccursor
					.getColumnIndex(MediaStore.Audio.Media.SIZE);
			int name = musiccursor
					.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME);

			ContentValues values = new ContentValues();

			values.put(MediaStore.MediaColumns.DATA,"/" + musiccursor.getString(name));
			values.put(MediaStore.MediaColumns.TITLE,musiccursor.getString(name));
			values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
			values.put(MediaStore.MediaColumns.SIZE, music_column_index);
			values.put(MediaStore.Audio.Media.ARTIST, "Artist");
			values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
			values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
			values.put(MediaStore.Audio.Media.IS_ALARM, false);
			values.put(MediaStore.Audio.Media.IS_MUSIC, false);

			Uri uri = MediaStore.Audio.Media.getContentUriForPath("/"
					+ musiccursor.getString(name));
			Uri newUri = getContentResolver().insert(uri, values);

			RingtoneManager.setActualDefaultRingtoneUri(MlistActivity.this,
					RingtoneManager.TYPE_RINGTONE, newUri);

			try {

				if (mMediaPlayer.isPlaying()) {
					mMediaPlayer.reset();
				}

				mMediaPlayer.setDataSource(filename);
				mMediaPlayer.prepare();
				mMediaPlayer.start();
			}

			catch (Exception e) {

			}

		}
	};

	public class MusicAdapter extends BaseAdapter {
		private Context mContext;

		public MusicAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return count;
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			System.gc();
			TextView tv = new TextView(mContext.getApplicationContext());
			String id = null;
			if (convertView == null) {
				music_column_index = musiccursor
						.getColumnIndex(MediaStore.Audio.Media.DATA);
				musiccursor.moveToPosition(position);
				id = musiccursor.getString(music_column_index);
				// music_column_index =
				// musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE);
				// musiccursor.moveToPosition(position);
				// id += " Type" + musiccursor.getString(music_column_index);
				tv.setText(id);
			} else
				tv = (TextView) convertView;
			return tv;
		}
	}
}